home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Flock 0.9.1.3 stable / flock-0.9.1.3.en-US.win32.exe / flock / components / flockLoggingConsoleWriter.js < prev    next >
Text File  |  2007-10-12  |  5KB  |  142 lines

  1. //
  2. // BEGIN FLOCK GPL
  3. // 
  4. // Copyright Flock Inc. 2005-2007
  5. // http://flock.com
  6. // 
  7. // This file may be used under the terms of of the
  8. // GNU General Public License Version 2 or later (the "GPL"),
  9. // http://www.gnu.org/licenses/gpl.html
  10. // 
  11. // Software distributed under the License is distributed on an "AS IS" basis,
  12. // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. // for the specific language governing rights and limitations under the
  14. // License.
  15. // 
  16. // END FLOCK GPL
  17. //
  18.  
  19. const CLASS_ID                = Components.ID("{442BDAE4-4FDC-4819-8748-C8659E415410}");
  20. const CLASS_NAME              = "Flock Logging Console Writer";
  21. const CONTRACT_ID             = "@flock.com/logging-console-writer;1";
  22.  
  23. function flockLoggingConsoleWriter()
  24. {
  25.   this._init();
  26. }
  27.  
  28. flockLoggingConsoleWriter.prototype = {
  29.  
  30.   _init: function() {
  31.     this._converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  32.     this._converter.charset = "UTF-8";
  33.   },
  34.  
  35.   emit: function(aDate, aLevel, aContext, aMessage)
  36.   {
  37.       var levels = new Array("all","debug", "info", "warn", "error", "fatal");
  38.       var date = new Date(aDate);
  39.       var dateString = this._pad(date.getHours(), 2) + ":" + this._pad(date.getMinutes(), 2) + ":" + this._pad(date.getSeconds(), 2) + "." + this._pad(date.getTime() % 1000, 3);
  40.     var content = "[" + dateString + " flock:" + aContext + ":" + levels[aLevel] + "] " + aMessage + "\n";
  41.     content = this._converter.ConvertFromUnicode(content) + this._converter.Finish();
  42.     dump(content);
  43.   },
  44.   
  45.   _pad: function(aNumber, aPlaces)
  46.   {
  47.     var numberString = aNumber + "";
  48.     while (numberString.length < aPlaces) {
  49.       numberString = "0" + numberString;
  50.     }
  51.     return numberString;
  52.   },
  53.     
  54.   // nsIClassInfo
  55.   getInterfaces: function(aCount)
  56.   {
  57.     var interfaces = [Components.interfaces.flockILoggingObserver, Components.interfaces.nsIClassInfo];
  58.     aCount.value = interfaces.length;
  59.     return interfaces;
  60.   },
  61.  
  62.   // nsIClassInfo
  63.   getHelperForLanguage: function(aLanguage)
  64.   {
  65.     return null;
  66.   },
  67.  
  68.   // nsIClassInfo
  69.   contractID: CONTRACT_ID,
  70.  
  71.   // nsIClassInfo
  72.   classDescription: CLASS_NAME,
  73.  
  74.   // nsIClassInfo
  75.   classID: CLASS_ID,
  76.  
  77.   // nsIClassInfo
  78.   implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  79.  
  80.   // nsIClassInfo
  81.   flags: Components.interfaces.nsIClassInfo.SINGLETON,
  82.   
  83.   // nsISupports
  84.   QueryInterface: function(aIID)
  85.   {
  86.     if (!aIID.equals(Components.interfaces.nsISupports) && !aIID.equals(Components.interfaces.flockILoggingObserver) && !aIID.equals(Components.interfaces.nsIClassInfo))
  87.       throw Components.results.NS_ERROR_NO_INTERFACE;
  88.     return this;
  89.   }
  90.  
  91. };
  92.  
  93. /******************************************************************************
  94.  * XPCOM Functions for construction and registration
  95.  ******************************************************************************/
  96. var Module = {
  97.   _firstTime: true,
  98.   registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  99.   {
  100.     if (this._firstTime) {
  101.       this._firstTime = false;
  102.       throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  103.     }
  104.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  105.     aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
  106.     
  107.     // Make the Logging Observer a default
  108.     var categoryManager = Components.classes["@mozilla.org/categorymanager;1"]
  109.       .getService(Components.interfaces.nsICategoryManager);
  110.     categoryManager.addCategoryEntry("flockILoggingObserver", CLASS_NAME, CONTRACT_ID, true, true, null);
  111.   },
  112.  
  113.   unregisterSelf: function(aCompMgr, aLocation, aType)
  114.   {
  115.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  116.     aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);        
  117.   },
  118.   
  119.   getClassObject: function(aCompMgr, aCID, aIID)
  120.   {
  121.     if (!aIID.equals(Components.interfaces.nsIFactory))
  122.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  123.     if (aCID.equals(CLASS_ID))
  124.       return Factory;
  125.     throw Components.results.NS_ERROR_NO_INTERFACE;
  126.   },
  127.  
  128.   canUnload: function(aCompMgr) { return true; }
  129. };
  130.  
  131. var Factory = {
  132.   createInstance: function(aOuter, aIID)
  133.   {
  134.     if (aOuter != null)
  135.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  136.     return (new flockLoggingConsoleWriter()).QueryInterface(aIID);
  137.   }
  138. };
  139.  
  140. function NSGetModule(aCompMgr, aFileSpec) { return Module; }
  141.  
  142.